package web;

import com.alibaba.druid.util.StringUtils;
import pojo.PageData;
import pojo.Student;
import service.StudentService;
import service.StudentServiceImpl;
import utils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
@WebServlet("/studentoperater")
public class StudentServlet extends HttpServlet {

    public StudentService studentService=new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String option = req.getParameter("option");
        switch(option){
            case"add":
                addStudents(req,resp);
                break;

            case"update":
                //更新
                break;

            case"delete":
                deleteById(req,resp);
                break;

            case"querylist"://查询列表
                queryList(req,resp);
                break;

            case"queryid"://查询单个数据
                break;

        }
    }
    private void deleteById(HttpServletRequest req,HttpServletResponse resp){
        String stuId = req.getParameter("stuId");//必须传递学生id
        if(StringUtils.isEmpty(stuId)){
            try {
                resp.getWriter().write("没有传递学生id，无法删除");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;

        }

        try {
            long id = Long.parseLong(stuId);
            studentService.deleteStudentById(id);
            //重新回到列表刷新一下
            resp.sendRedirect("./studentoperater?option=querylist");
        } catch (IOException e) {
            try {
                resp.getWriter().write("传递的学生id不是合法的");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }

    }

    //查询分页数据
    private void queryList(HttpServletRequest req,HttpServletResponse resp){
        //查询分页数据,需要告诉我当前是第几页,每页显示多少条

        String pageNum = req.getParameter("pageNum");//第几页
        String pageSize = req.getParameter("pageSize");//每页显示多少条

        int num=1;
        int size=5;

        if(!StringUtils.isEmpty(pageNum)){
            try{
                num=Integer.parseInt(pageNum);
            }catch (Exception e){
                e.printStackTrace();

            }

        }
        if(!StringUtils.isEmpty(pageSize)){
            try{
                size=Integer.parseInt(pageSize);
            }catch (Exception e){
                e.printStackTrace();

            }

        }

        PageData<Student> pageData = studentService.findStudentsByPage(num,size);//我们要显示的数据
        //有了数据之后是不是要显示数据,需要先确定在什么页面显示
        req.setAttribute("pagedata",pageData);
        try {
            req.getRequestDispatcher("/student/studentslist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addStudents(HttpServletRequest req,HttpServletResponse resp){

        try {
            Student student = BeanUtils.processParamters(req, Student.class);
            System.err.println("student===>"+student);
            studentService.addStudent(student);
            resp.getWriter().write("成功");
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
